<?php 

class Admin extends La_Controller {
	/**
	 * 登录
	 */
	public function login() {
		$user_ip = $this->input->ip_address();
		$this->load->model("Setting_model");
		$ip_permission_array = $this->Setting_model->find_by_key('backend_ip_permission');
		
		if (!empty($ip_permission_array)) {
			$ip_permission = $ip_permission_array['setting_value'];
			$ips = split("\n", $ip_permission);
			$allow = 0;
			foreach($ips as $ip) {
				$ip = trim(str_replace(".", "\.", $ip));
				$ip = str_replace("*", ".*", $ip);
				if(empty($ip)) continue;
				$pattern = '/' . $ip . '/';
				if (preg_match($pattern, $user_ip)) {
					$allow = 1;
					break;
				}
			}
			if($allow == 0) {
				redirect("/error/404");
			}
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$answer = $this->input->post("answer");
			$result = $this->authorize->admin_login() ;
			if ($result !=1) {
				$this->data['error'] = "用户名和密码错误";
			} else {
				la_redirect("/console/settings", "登录成功");
			}
		} 
		$this->load->view('/admin/login', $this->data);

		
	}
	
	/**
	 * 禁访ip
	 */
	public function ip_forbidden() {
		$this->load->view('admin/ip_forbidden');
	} 
	
	/**
	 * 后台访问ip
	 */
	public function ip_permission() {
		$this->load->view('admin/ip_permission');
	}
	
	/**
	 * 后台滚动信息
	 */
	public function story_scroller() {
		$this->load->view('admin/story_scroller');
	}
}